package com.hand.qiandu.Algorithm;

/**
 * @author 24748  刘瑞锋
 * @date 2019/7/19
 */
public class MKP {
    // 构造 pattern 的最大匹配数表
     int[] calculateMaxMatchLengths(String pattern) {
        int[] maxMatchLengths = new int[pattern.length()];
        int maxLength = 0;
        for (int i = 1; i < pattern.length(); i++) {
            while (maxLength > 0 && pattern.charAt(maxLength) != pattern.charAt(i)) {
                maxLength = maxMatchLengths[maxLength - 1]; // ①
            }
            if (pattern.charAt(i) == pattern.charAt(maxLength)) {
                maxLength++; // ②
            }
            maxMatchLengths[i] = maxLength;
        }
        return maxMatchLengths;
    }

    public static String replaceSpace2(StringBuffer str) {

        return str.toString().replaceAll("\\s", "%20");
    }

    public static void main(String[] args) {
//        String str = "ABCDABD";
//        MKP mkp = new MKP();
//        int[] result = mkp.calculateMaxMatchLengths(str);
//        for(int i = 0; i < result.length; i++){
//            System.out.print(result[i]);
//        }
//        System.out.println(result[0]);
//        System.out.println(result[5]);
        StringBuffer buffer = new StringBuffer("we are china          ");
        System.out.println(replaceSpace2(buffer));

    }
}
